package 队列;

import java.util.Stack;

/**
 * https://leetcode-cn.com/problems/implement-queue-using-stacks/
 * @author Mxb
 * @version 1.0
 * @date 2020/3/2 13:33
 */
public class _232_用栈实现队列 {
    /** Initialize your data structure here. */
    // 准备2个栈
    private Stack<Integer> inStack;
    private Stack<Integer> outStack;


    public _232_用栈实现队列() {
        inStack = new Stack<>();
        outStack = new Stack<>();
    }

    /** Push element x to the back of queue. */
    public void push(int x) {
        // 入队
        inStack.push(x);
    }

    /** Removes the element from in front of queue and returns that element. */
    public int pop() {
        checkStack();
        return outStack.pop();
    }

    /** Get the front element. */
    public int peek() {
        checkStack();
        return outStack.peek();
    }

    /** Returns whether the queue is empty. */
    public boolean empty() {
        //if(inStack.isEmpty() && outStack.isEmpty()){
        //    return true;
        //}else{
        //    return false;
        //}
        return inStack.isEmpty() && outStack.isEmpty();
    }

    private void checkStack(){
        // 判断 出栈是否为空
        if(outStack.isEmpty()){
            // 循环 出栈inStack
            while(!inStack.isEmpty()) {
                outStack.push(inStack.pop());
            }
        }
    }
}
